<?
/*
function GetConnection()
{        
        $connect = new mysqli( 'localhost', 'plotkinm', 'FaceBooK', 'plotkinm_db' );
        if($connect->error)
        {
                throw new Exception($connect->error, 1);
        }
        return $connect;
}



function GetZipCodes()
{
        $connect = GetConnection();
        return $connect->query('SELECT zip, city, state FROM US_Zip_Codes WHERE zip	LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" ORDER BY zip ASC LIMIT 0,10');
}

    if ( !isset($_REQUEST['term']) )
	exit;
	
	    $results = GetZipCodes();
		if ( $connect && mysql_num_rows($connect) )
		{
                        $data = array();
                        while($row = $results->fetch_assoc())
                                $data[] = array(
									'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] ,
									'value' => $row['zip']
								);
								
                                echo json_encode($data);
                        flush();
        }
 * 
 * 
 * Okay, so I can understand how the code at the bottom works but what I am having trouble with is figuring out how to reformat it to fit with the code was have been working with during the semester
*/
// if the 'term' variable is not sent with the request, exit
if ( !isset($_REQUEST['term']) )
	exit;
// connect to the database server and select the appropriate database for use
$dblink = mysql_connect('localhost', 'plotkinm', 'FaceBooK') or die( mysql_error() );
mysql_select_db('plotkinm_db');

// query the database table for zip codes that match 'term'
$rs = mysql_query('SELECT zip, city, state FROM US_Zip_Codes WHERE zip AND city LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" ORDER BY zip ASC LIMIT 0,10', $dblink);

// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
	while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
	{
		$data[] = array(
			'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] ,
			'value' => $row['zip']
		);
	}
}

// jQuery wants JSON data
echo json_encode($data);
flush();

